package com.Babysleep.manage;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageButton;

import com.Babysleep.R;
import com.Babysleep.Activity.BasicActivity;
import com.Babysleep.Interface.ListenHandler;

/**
 * 负责录音播放，以及UI控制。
 * 
 */
/**
 * 音频播放器的基类
 * 
 */
public class Listen extends MediaPlayer {
	BasicActivity activity;
	protected boolean is_pausing = false;
	protected boolean playing_started = false;
	protected ListenHandler listenHandler;
	public int counter = 0;
	protected ImageButton button;
	private float default_volum;
	private float current_volum;
	private AudioManager audioManager;

	protected SimpleDateFormat df = new SimpleDateFormat("mm:ss");

	public Listen(BasicActivity activity) {
		super();
		this.activity = activity;
		df.setTimeZone(TimeZone.getTimeZone("GMT+0"));
		audioManager = (AudioManager) activity
				.getSystemService(Context.AUDIO_SERVICE);

		current_volum = 0;
	}

	public void init(ImageButton button,String path) {
		try {
			setDataSource(path);
			setLooping(true);
			prepare();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e);
		}
		this.button = button;
	}

	public void count() {
		counter = counter - Constant.delay_length;
	}

	public void set_counter(int counter) {
		this.counter = counter * 1000;
	}

	public void Play() {
		System.out.println(String.valueOf(playing_started)
				+ String.valueOf(is_pausing));
		if (playing_started == false) {
			button.setImageDrawable(activity.getResources().getDrawable(
					R.drawable.pause));
			playing_started = true;
			counter = activity.cycle_length * 1000;
			System.out.println(counter);
			try {
				start();
				volum_up();
			} catch (Exception e) {
				System.out.println(e.toString());
				// TODO: handle exception
			}
		} else if (is_pausing == true) {
			button.setImageDrawable(activity.getResources().getDrawable(
					R.drawable.pause));
			Restart();
		} else if (is_pausing == false) {
			button.setImageDrawable(activity.getResources().getDrawable(
					R.drawable.start));
			Pause();
		}
	}

	public void Restart() {
		button.setImageDrawable(activity.getResources().getDrawable(
				R.drawable.pause));
		is_pausing = false;
		start();
		volum_up();
	}

	public void Pause() {
		button.setImageDrawable(activity.getResources().getDrawable(
				R.drawable.start));
		is_pausing = true;
		volum_down();
	}

	public void Stop() {
		stop();
		button.setImageDrawable(activity.getResources().getDrawable(
				R.drawable.start));
		is_pausing = false;
		playing_started = false;
		try {
			setLooping(true);
			prepare();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// listenHandler.stop();
	}

	public void volum_up() {
		current_volum = 0;
		default_volum = audioManager
				.getStreamMaxVolume(AudioManager.STREAM_RING);
		new VolumHandler().sendEmptyMessageDelayed(1, 100);
	}

	public void volum_down() {
		default_volum = audioManager
				.getStreamMaxVolume(AudioManager.STREAM_RING);
		current_volum = default_volum;
		new VolumHandler().sendEmptyMessageDelayed(0, 100);
	}

	class VolumHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				if (current_volum < default_volum) {
					current_volum = current_volum
							+ (float) (default_volum / 10.0);
					setVolume(current_volum, current_volum);
					System.out.println(current_volum
							+ String.valueOf((float) (default_volum / 10.0)));
					sendEmptyMessageDelayed(1, 100);
				} else {
					setVolume(default_volum, default_volum);
				}
				break;
			case 0:
				if (current_volum > 0) {
					current_volum = current_volum
							- (float) (default_volum / 10.0);
					setVolume(current_volum, current_volum);
					System.out.println(current_volum
							+ String.valueOf((float) (default_volum / 10.0)));
					sendEmptyMessageDelayed(0, 100);
				} else {
					pause();
					setVolume(default_volum, default_volum);
				}
			default:
				break;
			}
		}

	}

	public void init(ImageButton button, int path_id) {
		
	}

}
